package com.cheng.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.CourierDao;
import com.cheng.dao.FixedAreaRepository;
import com.cheng.dao.TaketimeDao;
import com.cheng.domain.base.Courier;
import com.cheng.domain.base.FixedArea;
import com.cheng.domain.base.TakeTime;
import com.cheng.service.base.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
	
	//注入dao
	@Autowired
	private FixedAreaRepository fixedAreaRepository;

	//保存定区
	@Override
	public void saveFixedArea(FixedArea model) {
		fixedAreaRepository.save(model);
	}
	
	//删除定区
	@Override
	public void delFixedArea(String id) {
		fixedAreaRepository.delete(id);
	}

	//分页查找定区
	@Override
	public Page<FixedArea> searchFixedArea(
			Specification<FixedArea> specification, Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}
	
	
	//注入快递员dao
	@Autowired
	private CourierDao courierDao;
	//注入排版时间dao
	@Autowired
	private TaketimeDao taketimeDao;
	//关联快递员和排班时间,到定区
	@Override
	public void associationCourierAndTaketime(FixedArea model,
			Integer courierId, Integer takeTimeId) {
		//获取定区
		System.out.println(model.getId()+"定区编号/////////////////////////////");
		FixedArea fixedarea = fixedAreaRepository.findOne(model.getId());
		//获取快递员
		Courier courier = courierDao.findOne(courierId);
		//获取排班信息
		TakeTime taketime = taketimeDao.findOne(takeTimeId);
		//将快递员添加到定区
		fixedarea.getCouriers().add(courier);
		//为快递员设置取派时间
		courier.setTakeTime(taketime);
	}

	@Override
	public List<FixedArea> getAllFixedArea() {
		return fixedAreaRepository.findAll();
	}

	@Override
	public FixedArea getAssociationsCourier(String id) {
		return fixedAreaRepository.findOne(id);
	}

}
